package com.haibara.codesubmiteval.chain.userChain;

import cn.hutool.core.util.StrUtil;
import com.haibara.codesubmiteval.constant.AuthConstant;
import com.haibara.codesubmiteval.model.dto.user.UserAddRequest;
import com.haibara.codesubmiteval.model.entity.User;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 用户班级信息处理节点
 *
 * @Author: mengyu
 * @Date: 2025/4/28
 */
@Component
@Order(1)
public class ClassDefaultValueHandler extends CreateUserDefaultValueHandler {
    @Override
    public void handleDefaultValue(User user, UserAddRequest userAddRequest) {
        String userRole = userAddRequest.getUserRole();
        String clazz = userAddRequest.getClazz();
        Long id = userAddRequest.getId();
        if (AuthConstant.ADMIN.equals(userRole)) {
            user.setClazz("管理员-0班");
        } else {
            // 如果班级未设定，默认未学号中的班级号
            if (StrUtil.isBlankIfStr(clazz)) {
                user.setClazz(id.toString().substring(2, 8));
            } else {
                user.setClazz(clazz);
            }
        }
        if (next != null) next.handleDefaultValue(user, userAddRequest);
    }
}
